package com.jet.javatest.thread;

import java.util.concurrent.TimeUnit;

/**
 * Description:
 * File: SynchronizedTest.java
 * Author: Administrator
 * Create: 2019/11/7 13:57
 */
public class SynchronizedTest implements Runnable {
    private static Object MUTEX = new Object();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            SynchronizedTest synchronizedTest = new SynchronizedTest();
            new Thread(synchronizedTest).start();
        }
    }

    private static void test() {
        synchronized (MUTEX) {
            System.out.println("ThreadName = " + Thread.currentThread().getName() + ",stamp = " + System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(1L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        test();
    }
}
//    //1
//    @Override
//    public void run() {
//        synchronized (this) {
//            System.out.println("ThreadName1 = " + Thread.currentThread().getName());
//            try {
//                TimeUnit.SECONDS.sleep(5L);
//            } catch (InterruptedException e) {
//            }
//        }
//    }


//2
//    @Override
//    public synchronized void run() {
//            System.out.println("ThreadName2 = " + Thread.currentThread().getName());
//            try {
//                TimeUnit.SECONDS.sleep(5L);
//            } catch (InterruptedException e) {
//        }
//    }
//3
//    @Override
//    public void run() {
//        synchronized (MUTEX) {
//            System.out.println("ThreadName3 = " + Thread.currentThread().getName());
//            try {
//                TimeUnit.SECONDS.sleep(5L);
//            } catch (InterruptedException e) {
//            }
//        }
//    }
